import 'package:yw_bapp/model/del_menuItem_model.dart';
import 'package:yw_bapp/model/menu_detail_model.dart';
import 'package:yw_bapp/model/menu_list_model.dart';
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:yw_bapp/model/store_qr_code.dart';
import 'package:yw_bapp/utils/loacl_storage.dart';

const BASER_URL = "https://ywapidev.lemobj.com/api/";
var headerMap = new Map<String, String>.from(
    {"content-type": "application/json", "X-YOUWEI-PLAT": 'wx_b_user'});
// 获取门店二维码地址
const qrURL = BASER_URL + "manager/store/get_store_QrCode";
// 获取门店所有菜品地址
const menuListURL = BASER_URL + "manager/store/get_store_menuLists";
// 删除门店菜品地址
const delMenuURL = BASER_URL + "manager/store/delete_store_menu/";
// 获取某菜品详情地址
const menuDetailURL = BASER_URL + "manager/store/get_store_menu/";

// 门店，商户相关的接口
class MerchantDao {
  // 获取订单列表接口
  static Future<StoreQrCodeModel> GetQrCode() async {
    var storeID = await getStorage('Store-ID');
    var token = await getStorage('Token');
    // headerMap['Token'] = token.toString();
    // headerMap['Store-ID'] = storeID.toString();
    headerMap['Token'] = '529';
    headerMap['Store-ID'] = '226';
    final response = await http.get(qrURL, headers: headerMap);
    if (response.statusCode == 200) {
      // 解决中文乱码
      Utf8Decoder utf8decoder = Utf8Decoder();
      var result = json.decode(utf8decoder.convert(response.bodyBytes));
      return StoreQrCodeModel.fromJson(result);
    } else {
      print('请求接口失败');
      throw Exception('请求接口失败');
    }
  }

  // 获取门店的所有菜品信息
  static Future<MenuListModel> GetMenuList() async {
    var storeID = await getStorage('Store-ID');
    var token = await getStorage('Token');
    // headerMap['Token'] = token.toString();
    // headerMap['Store-ID'] = storeID.toString();
    headerMap['Token'] = '529';
    headerMap['Store-ID'] = '226';
    final response = await http.get(menuListURL, headers: headerMap);
    if (response.statusCode == 200) {
      // 解决中文乱码
      Utf8Decoder utf8decoder = Utf8Decoder();
      var result = json.decode(utf8decoder.convert(response.bodyBytes));
      return MenuListModel.fromJson(result);
    } else {
      print('请求接口失败');
      throw Exception('请求接口失败');
    }
  }

  // 删除菜品信息
  static Future<DelMenuItemModel> DeleteMenuItem(id) async {
    var storeID = await getStorage('Store-ID');
    var token = await getStorage('Token');
    // headerMap['Token'] = token.toString();
    // headerMap['Store-ID'] = storeID.toString();
    headerMap['Token'] = '529';
    headerMap['Store-ID'] = '226';
    print(delMenuURL + '$id');
    final response = await http.delete(delMenuURL + '$id', headers: headerMap);
    if (response.statusCode == 200) {
      // 解决中文乱码
      Utf8Decoder utf8decoder = Utf8Decoder();
      var result = json.decode(utf8decoder.convert(response.bodyBytes));
      return DelMenuItemModel.fromJson(result);
    } else {
      print('请求接口失败');
      throw Exception('请求接口失败');
    }
  }

  // 获取某菜品详情
  static Future<MenuDatailModel> GetMenuItemDetail(id) async {
    var storeID = await getStorage('Store-ID');
    var token = await getStorage('Token');
    // headerMap['Token'] = token.toString();
    // headerMap['Store-ID'] = storeID.toString();
    headerMap['Token'] = '529';
    headerMap['Store-ID'] = '226';
    print(menuDetailURL + '$id');
    final response = await http.get(menuDetailURL + '$id', headers: headerMap);
    if (response.statusCode == 200) {
      // 解决中文乱码
      Utf8Decoder utf8decoder = Utf8Decoder();
      var result = json.decode(utf8decoder.convert(response.bodyBytes));
      return MenuDatailModel.fromJson(result);
    } else {
      print('请求接口失败');
      throw Exception('请求接口失败');
    }
  }
}
